package com.spt.ws.task;

import java.util.List;
import java.util.Map;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import com.spt.ws.dao.ITaskService;

public class TaskManager {
    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;
    @Autowired
    private ITaskService taskService;

    public void initTask() throws Exception {
        Scheduler scheduler = schedulerFactoryBean.getScheduler();
        // 可执行的任务列表
        List<Map<String, Object>> taskList = taskService.getAllTask();
        for (Map<String, Object> task : taskList) {
            // 任务名称和任务组设置规则：
            // 名称：task_1 ..
            // 组 ：group_1 ..
            if("n".equalsIgnoreCase((String)task.get("LASTSTATUS"))) continue;
            TriggerKey triggerKey = TriggerKey.triggerKey((String) task.get("TASKID"), (String) task.get("GROUPID"));
            CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
            // 不存在，创建一个
            if (null == trigger) {
                JobDetail jobDetail = JobBuilder.newJob(QuartzTaskFactory.class)
                        .withIdentity((String) task.get("TASKID"), (String) task.get("GROUPID")).build();
                jobDetail.getJobDataMap().put("scheduleJob", task);
                // 表达式调度构建器
                CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule((String) task.get("CRON"));
                // 按新的表达式构建一个新的trigger
                trigger = TriggerBuilder.newTrigger()
                        .withIdentity((String) task.get("TASKID"), (String) task.get("GROUPID"))
                        .withSchedule(scheduleBuilder).build();
                scheduler.scheduleJob(jobDetail, trigger);
            } else {
                // trigger已存在，则更新相应的定时设置
                CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule((String) task.get("CRON"));
                // 按新的cronExpression表达式重新构建trigger
                trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
                // 按新的trigger重新设置job执行
                scheduler.rescheduleJob(triggerKey, trigger);
            }
        }
    }

}
